$(function(){

    // 登录界面密码显隐切换按钮
    $(".password").on("click", ".fa-eye-slash", function () {
        $(this).removeClass("fa-eye-slash").addClass("fa-eye");
        $(this).prev().attr("type", "text");
    });
    $(".password").on("click", ".fa-eye", function () {
        $(this).removeClass("fa-eye").addClass("fa-eye-slash");
        $(this).prev().attr("type", "password");
    });

    //checkbox: selected all
    $("[data-toggle='checkbox']").click(function(){
        var target=$(this).attr("data-target");
        var result=this.checked;
        $("[name="+target+"]").each(function(){
            if(!$(this).attr("disabled")){
                this.checked=result;
            }
        });
    });

    // least one modal or confirm modal
    var leastOneHtml = '<div class="modal fade alert-modal" id="leastOneModal" role="dialog"aria-labelledby="leastOneLabel" aria-hidden="true"> ' +
        '<div class="modal-dialog"> ' +
        '<div class="modal-content"> ' +
        '<div class="modal-header"> ' + '<button type="button" class="close" data-dismiss="modal"> ' + '<span aria-hidden="true">&times;</span> ' + '</button> ' + '<h4 class="modal-title" id="leastOneLabel">提示</h4> ' + '</div> ' +
        '<div class="modal-body text-center"> <span class="result-txt">请至少选择一项！</span> </div> ' +
        '</div> </div> </div>';

    var confirmHtml = '<div class="modal fade alert-modal" id="confirmModal" role="dialog" aria-labelledby="confirmLabel" aria-hidden="true"> ' +
        '<div class="modal-dialog"> ' +
        '<div class="modal-content"> ' +
        '<div class="modal-header"> <button type="button" class="close" data-dismiss="modal"> <span aria-hidden="true">&times;</span> </button> <h4 class="modal-title" id="confirmLabel">提示</h4> </div> ' +
        '<div class="modal-body text-center"> <span class="result-txt"></span> </div> ' +
        '</div> </div> </div>';

    var leastOneHtml2 = '<div class="modal fade alert-modal" id="leastOneModal2" role="dialog"aria-labelledby="leastOneLabel" aria-hidden="true"> ' +
        '<div class="modal-dialog"> ' +
        '<div class="modal-content"> ' +
        '<div class="modal-header"> ' + '<button type="button" class="close" data-dismiss="modal"> ' + '<span aria-hidden="true">&times;</span> ' + '</button> ' + '<h4 class="modal-title" id="leastOneLabel">提示</h4> ' + '</div> ' +
        '<div class="modal-body text-center"> <span class="result-txt">请至少选择一项！</span> </div> ' +
        '</div> </div> </div>';

    var confirmHtml2 = '<div class="modal fade alert-modal" id="confirmModal2" role="dialog" aria-labelledby="confirmLabel" aria-hidden="true"> ' +
        '<div class="modal-dialog"> ' +
        '<div class="modal-content"> ' +
        '<div class="modal-header"> <button type="button" class="close" data-dismiss="modal"> <span aria-hidden="true">&times;</span> </button> <h4 class="modal-title" id="confirmLabel">提示</h4> </div> ' +
        '<div class="modal-body text-center"> <span class="result-txt"></span> </div> ' +
        '<div class="modal-footer text-center"> <button type="button" class="btn btn-primary">确定</button> <button type="button" class="btn btn-default" data-dismiss="modal">取消</button> </div> ' +
        '</div> </div> </div>';

    if($('[data-toggle="leastOne"]').length){
        $('body').append(confirmHtml);
        $('body').append(leastOneHtml);
    }

    if($('[data-toggle="leastOneCom"]').length){
        $('body').append(confirmHtml2);
        $('body').append(leastOneHtml2);
    }

    $('[data-toggle="leastOne"]').click(function() {
        var target = $(this).attr('data-target');
        if (getCheckedSum(target)) {
            $('#confirmModal').modal('show');
            var txt = $(this).attr('data-txt');
            $('.result-txt').html(txt);
        } else {
            $('#leastOneModal').modal('show');
        }
    });

    $('[data-toggle="leastOneCom"]').click(function() {
        var target = $(this).attr('data-target');
        if (getCheckedSum(target)) {
            $('#confirmModal2').modal('show');
            var txt = $(this).attr('data-txt');
            $('.result-txt').html(txt);
        } else {
            $('#leastOneModal2').modal('show');
        }
    });

    $('[data-toggle="confirm"]').click(function() {
        $('#confirmModal').modal('show');
        var txt = $(this).attr('data-txt');
        $('.result-txt').html(txt);
    });

    // 检测屏幕窗口宽度，小于769时使用mini状态
    $(window).on('load resize',function(){
        if ($(window).width() < 769) {
            $('.sidebar-nav').addClass('nav-mini');
            $('header').addClass('header-mini');
            $('.main').addClass('main-mini');
            $('.sidebar-nav .text').addClass('hide');
        } else {
            $('.sidebar-nav').removeClass('nav-mini');
            $('header').removeClass('header-mini');
            $('.main').removeClass('main-mini');
            $('.sidebar-nav .text').removeClass('hide');
        }
    });

    // nav-mini 切换
    $('#mini').on('click',function(){
        if (!$('.sidebar-nav').hasClass('nav-mini')) {
            $('.nav-item.nav-show').removeClass('nav-show');
            $('.nav-item').children('ul').removeAttr('style');
            $('.sidebar-nav').addClass('nav-mini');
            $('header').addClass('header-mini');
            $('.main').addClass('main-mini');
            $('.sidebar-nav .text').addClass('hide');
        }else{
            $('.sidebar-nav').removeClass('nav-mini');
            $('header').removeClass('header-mini');
            $('.main').removeClass('main-mini');
            $('.sidebar-nav .text').removeClass('hide');
        }
    });

    // 侧导航点击展开二级收缩切换
    $('.nav-item>a').on('click',function(){
        if (!$('.sidebar-nav').hasClass('nav-mini')) {
            if ($(this).next().css('display') == "none"||$(this).next().length==0) {
                //展开未展开
                $('.nav-item').children('ul').slideUp(300);
                $(this).next('ul').slideDown(300);
                $(this).parent('li').addClass('nav-show').siblings('li').removeClass('nav-show');
            }else{
                //收缩已展开
                $(this).next('ul').slideUp(300);
                $('.nav-item.nav-show').removeClass('nav-show');
            }
        }
    });

    // 侧导航点击高亮显示
    $('.nav-list li a').on('click',function(event){
        $('.nav-list li a').removeClass('active');
        $(this).addClass('active');
        if(window.event){
            event.cancelBubble  = true;
        }else{
            event.stopPropagation();
        }
    });

    // 用户下拉dropdown
    $('.header .user-content>.img-box').on('click',function(){
        if($('.header .user-content>.dropdown-ul').hasClass('hide')){
            $('.header .user-content>.dropdown-ul').removeClass('hide');
        }
        else{
            $('.header .user-content>.dropdown-ul').addClass('hide');
        }
    });

    $('.header .user-content .item').on('click',function(){
        $('.header .user-content>.dropdown-ul').addClass('hide');
    });

    // 初始化侧导航
    $('.sidebar-nav>ul').slimScroll({
        height: 'calc(100% - 63px)',
        color: '#ddd',
        railOpacity: 0.9,
        railColor: '#333',
        size: '5px'
    });

});

//获取name值相同多选框已选中的个数
function getCheckedSum(name) {
    var sum = 0;
    $('[type="checkbox"][name='+name+']').each(function() {
        if(this.checked) sum++;
    });
    return sum
}